package com.qys.livesMall.navy.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qys.livesMall.app.user.model.entity.H2AppUser;
import com.qys.livesMall.app.user.model.entity.H2AppUserAddress;
import com.qys.livesMall.app.user.service.IH2AppUserAddressService;
import com.qys.livesMall.app.user.service.IH2AppUserService;
import com.qys.livesMall.common.exception.QysException;
import com.qys.livesMall.common.exception.enums.ExceptionEnum;
import com.qys.livesMall.common.message.MessageServer;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.common.utils.ip.IpUtil;
import com.qys.livesMall.navy.model.dto.*;
import com.qys.livesMall.navy.service.NavyUserServer;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * 后台-水军用户操作
 *
 * @date 2023/07/03 11:45
 */
@RestController
@RequestMapping("/sys/navy/user/")
public class NavyUserController {

    @Autowired
    private NavyUserServer navyUserServer;

    @Resource
    private MessageServer messageServer;

    @Resource
    private IH2AppUserService appUserService;

    @Resource
    private IH2AppUserAddressService ih2AppUserAddressService;

    /**
     * 新增
     *
     * @param addNavy
     * @return
     */
    @PostMapping("add")
    public R add(@RequestBody AddNavy addNavy,HttpServletRequest request) {
        String ip = IpUtil.getIpAddr(request);
        return navyUserServer.add(addNavy,ip);
    }

    /**
     * 编辑信息
     *
     * @param
     * @return
     */
    @PostMapping("edit")
    public R edit(@RequestBody EditNavyUserInfo navyUserInfo) {
        if (StringUtils.isAnyBlank(navyUserInfo.getUserId(), navyUserInfo.getUserName(), navyUserInfo.getEmail())) {
            return messageServer.failMsg(ExceptionEnum.PARAM_IS_ERROR);
        }
        H2AppUser user = new H2AppUser();
        user.setId(navyUserInfo.getUserId());
        user.setNickName(navyUserInfo.getUserName());
        user.setAvatar(navyUserInfo.getPic());
        user.setEmail(navyUserInfo.getEmail());
        user.setUpdateTime(LocalDateTime.now());
        appUserService.updateById(user);
        return R.ok();
    }

    /**
     * 修改名称
     *
     * @param navyUserName
     * @return
     */
    @PostMapping("editName")
    public R editName(@RequestBody EditNavyUserName navyUserName) {
        return navyUserServer.editName(navyUserName);
    }

    /**
     * 赠送/扣除余额
     *
     * @param userBalance
     * @return
     */
    @PostMapping("editBalance")
    public R editBalance(@RequestBody EditNavyUserBalance userBalance) {
        return navyUserServer.editBalance(userBalance);
    }

    /**
     * 兑换金币
     *
     * @param userGold
     * @return
     */
    @PostMapping("editGold")
    public R editGold(@RequestBody EditNavyUserGold userGold) {
        return navyUserServer.editGold(userGold);
    }

    /**
     * 修改头像
     *
     * @param userPic
     * @return
     */
    @PostMapping("editPics")
    public R editPics(@RequestBody EditNavyUserPic userPic) {
        return navyUserServer.editPics(userPic);
    }

    /**
     * 批量开店
     *
     * @param openShop
     * @return
     */
    @PostMapping("openShops")
    public R openShops(@RequestBody OpenShop openShop) {
        return navyUserServer.openShop(openShop.getUserIds());
    }


    /**
     * 批量主播
     *
     * @param openShop
     * @return
     */
    @PostMapping("openAnchor")
    public R openAnchor(@RequestBody OpenShop openShop) {
        return navyUserServer.openAnchor(openShop.getUserIds());
    }


}

